using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OrmTestDomain;
using ShellProgressBar;

namespace FreeSqlTest.BackgroundServices;

public class FreeSqlDefaultTestService(ILogger<FreeSqlDefaultTestService> logger, IFreeSql freeSql)
    : BackgroundService
{
    private readonly ILogger<FreeSqlDefaultTestService> _logger = logger;
    private readonly IFreeSql _freeSql = freeSql;


    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var testList = PrepareTestData();
        Insert(testList);
        Find();
        Update(testList);
        Find();
        // Delete(testList);
        // Find();
        BulkInsert(testList);
        Find();
        return Task.CompletedTask;
    }

    private IList<UserEntity> PrepareTestData()
    {
        var result = new List<UserEntity>();
        var random = new Random();
        var randomNumber = random.Next(100, 200);
        var totalTicks = randomNumber;

        var progressBarOptions = new ProgressBarOptions { ProgressCharacter = '─', ProgressBarOnBottom = true };
        using (var bar = new ProgressBar(totalTicks, "Test Data Preparing....", progressBarOptions))
        {
            //增加前一天的数据
            for (var i = 1; i <= totalTicks; i++)
            {
                result.Add(new UserEntity(){CreateTime = DateTime.Now.AddDays(-1)});
                bar.Tick($" {i} of {totalTicks}");
                Task.Delay(100);
            }
            //增加当天的数据
            for (var i = 1; i <= totalTicks; i++)
            {
                result.Add(new UserEntity(){CreateTime = DateTime.Now});
                bar.Tick($" {i} of {totalTicks}");
                Task.Delay(100);
            }
        }

        return result;
    }


    public void BulkInsert(IList<UserEntity> list)
    {
        _freeSql.InsertOrUpdate<UserEntity>().SetSource(list).ExecuteSqlBulkCopy();
    }



    private void Insert(IList<UserEntity> list)
    {
        var totalTicks = list.Count;

        var progressBarOptions = new ProgressBarOptions { ProgressCharacter = '─', ProgressBarOnBottom = true };
        using (var bar = new ProgressBar(totalTicks, "Test Data Inserting....", progressBarOptions))
        {
            var i = 1;
            foreach (var item in list)
            {
                _freeSql.Insert(item).ExecuteAffrows();;
                bar.Tick($" {i} of {totalTicks}");
                i++;
            }
        }
    }

    public void Delete(IList<UserEntity> list)
    {
        var totalTicks = list.Count;

        var progressBarOptions = new ProgressBarOptions { ProgressCharacter = '─', ProgressBarOnBottom = true };
        using (var bar = new ProgressBar(totalTicks, "Test Data Deleting....", progressBarOptions))
        {
            var i =1;
            foreach (var item in list)
            {
                _freeSql.Delete<UserEntity>(item).ExecuteAffrows();;
                bar.Tick($" {i} of {totalTicks}");
                i++;
            }
        }
    }

    public void Update(IList<UserEntity> list)
    {
        var totalTicks = list.Count;

        var progressBarOptions = new ProgressBarOptions { ProgressCharacter = '─', ProgressBarOnBottom = true };
        using (var bar = new ProgressBar(totalTicks, "Test Data Updating....", progressBarOptions))
        {
            var i = 1;
            foreach (var item in list)
            {
                item.Name = item.Name + DateTime.Now.ToLongTimeString();
                _freeSql.Update<UserEntity>(item).ExecuteAffrows();;
                bar.Tick($" {i} of {totalTicks}");
                i++;
            }
        }
    }



    public void Find()
    {
     var list=   _freeSql.Select<UserEntity>().ToList<UserEntity>();
     Console.WriteLine(list.Count);
    }


}
